package ir.sourcecodes.animalsounds;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;
import java.util.ArrayList;
import java.util.List;

public class JustifiedTextView extends View {
    private int colIndex = 0;
    boolean hasTextBeenDrown = false;
    private int lineHeight;
    private List<String> lineList = new ArrayList();
    private int lineSpace = 0;
    private Context mContext;
    private XmlToClassAttribHandler mXmlParser;
    private int measuredViewHeight;
    private int measuredViewWidth;
    private int rowIndex = 0;
    private String text;
    private int textAreaWidth;
    private TextPaint textPaint;

    public JustifiedTextView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        constructor(context, attributeSet);
    }

    public JustifiedTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        constructor(context, attributeSet);
    }

    public JustifiedTextView(Context context) {
        super(context);
        constructor(context, (AttributeSet) null);
    }

    private void constructor(Context context, AttributeSet attributeSet) {
        this.mContext = context;
        this.mXmlParser = new XmlToClassAttribHandler(context, attributeSet);
        initTextPaint();
        if (attributeSet != null) {
            String textValue = this.mXmlParser.getTextValue();
            int colorValue = this.mXmlParser.getColorValue();
            int textSize = this.mXmlParser.getTextSize();
            int i = this.mXmlParser.gettextSizeUnit();
            setText(textValue);
            setTextColor(colorValue);
            if (i == -1) {
                setTextSize((float) textSize);
            } else {
                setTextSize(i, (float) textSize);
            }
        }
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                if (!JustifiedTextView.this.hasTextBeenDrown) {
                    JustifiedTextView.this.hasTextBeenDrown = true;
                    JustifiedTextView justifiedTextView = JustifiedTextView.this;
                    justifiedTextView.setTextAreaWidth(justifiedTextView.getWidth() - (JustifiedTextView.this.getPaddingLeft() + JustifiedTextView.this.getPaddingRight()));
                    JustifiedTextView.this.calculate();
                }
            }
        });
    }

    /* access modifiers changed from: private */
    public void calculate() {
        setLineHeight(getTextPaint());
        this.lineList.clear();
        List<String> divideOriginalTextToStringLineList = divideOriginalTextToStringLineList(getText());
        this.lineList = divideOriginalTextToStringLineList;
        setMeasuredDimentions(divideOriginalTextToStringLineList.size(), getLineHeight(), getLineSpace());
        measure(getMeasuredViewWidth(), getMeasuredViewHeight());
    }

    private void initTextPaint() {
        TextPaint textPaint2 = new TextPaint(1);
        this.textPaint = textPaint2;
        textPaint2.setTextAlign(Paint.Align.RIGHT);
    }

    /* access modifiers changed from: protected */
    public void onMeasure(int i, int i2) {
        if (getMeasuredViewWidth() > 0) {
            requestLayout();
            setMeasuredDimension(getMeasuredViewWidth(), getMeasuredViewHeight());
        } else {
            super.onMeasure(i, i2);
        }
        invalidate();
    }

    /* access modifiers changed from: protected */
    public void onDraw(Canvas canvas) {
        this.rowIndex = getPaddingTop();
        if (getAlignment() == Paint.Align.RIGHT) {
            this.colIndex = getPaddingLeft() + getTextAreaWidth();
        } else {
            this.colIndex = getPaddingLeft();
        }
        for (int i = 0; i < this.lineList.size(); i++) {
            this.rowIndex += getLineHeight() + getLineSpace();
            canvas.drawText(this.lineList.get(i), (float) this.colIndex, (float) this.rowIndex, getTextPaint());
        }
    }

    private List<String> divideOriginalTextToStringLineList(String str) {
        ArrayList arrayList = new ArrayList();
        String[] split = str.split("\n");
        String str2 = "";
        for (String split2 : split) {
            String[] split3 = split2.split(" ");
            int i = 0;
            while (i < split3.length) {
                str2 = str2 + split3[i] + " ";
                float measureText = getTextPaint().measureText(str2);
                if (((float) getTextAreaWidth()) == measureText) {
                    arrayList.add(str2);
                } else if (((float) getTextAreaWidth()) < measureText) {
                    str2 = str2.substring(0, (str2.length() - split3[i].length()) - 1);
                    if (str2.trim().length() == 0) {
                        i++;
                    } else {
                        arrayList.add(justifyTextLine(this.textPaint, str2.trim(), getTextAreaWidth()));
                        i--;
                    }
                } else if (i == split3.length - 1) {
                    arrayList.add(str2);
                } else {
                    i++;
                }
                str2 = "";
                i++;
            }
        }
        return arrayList;
    }

    private String justifyTextLine(TextPaint textPaint2, String str, int i) {
        float measureText = textPaint2.measureText(str);
        int i2 = 0;
        while (measureText < ((float) i) && measureText > 0.0f) {
            i2 = str.indexOf(" ", i2 + 2);
            if (i2 == -1) {
                int indexOf = str.indexOf(" ", 1);
                if (indexOf == -1) {
                    return str;
                }
                i2 = indexOf;
            }
            str = str.substring(0, i2) + "  " + str.substring(i2 + 1, str.length());
            measureText = textPaint2.measureText(str);
        }
        return str;
    }

    private void setLineHeight(TextPaint textPaint2) {
        Rect rect = new Rect();
        textPaint2.getTextBounds("www.wiaDevelopers.com", 0, 21, rect);
        setLineHeight(rect.height());
    }

    public void setMeasuredDimentions(int i, int i2, int i3) {
        setMeasuredViewHeight((i * (i2 + i3)) + i3 + getPaddingRight() + getPaddingLeft());
        setMeasuredViewWidth(getWidth());
    }

    private int getTextAreaWidth() {
        return this.textAreaWidth;
    }

    /* access modifiers changed from: private */
    public void setTextAreaWidth(int i) {
        this.textAreaWidth = i;
    }

    private int getLineHeight() {
        return this.lineHeight;
    }

    private int getMeasuredViewHeight() {
        return this.measuredViewHeight;
    }

    private void setMeasuredViewHeight(int i) {
        this.measuredViewHeight = i;
    }

    private int getMeasuredViewWidth() {
        return this.measuredViewWidth;
    }

    private void setMeasuredViewWidth(int i) {
        this.measuredViewWidth = i;
    }

    private void setLineHeight(int i) {
        this.lineHeight = i;
    }

    public String getText() {
        return this.text;
    }

    public void setText(String str) {
        this.text = str;
        calculate();
        invalidate();
    }

    public void setText(int i) {
        setText(this.mContext.getResources().getString(i));
    }

    public Typeface getTypeFace() {
        return getTextPaint().getTypeface();
    }

    public void setTypeFace(Typeface typeface) {
        getTextPaint().setTypeface(typeface);
    }

    public float getTextSize() {
        return getTextPaint().getTextSize();
    }

    public void setTextSize(int i, float f) {
        setTextSize(TypedValue.applyDimension(i, f, this.mContext.getResources().getDisplayMetrics()));
    }

    private void setTextSize(float f) {
        getTextPaint().setTextSize(f);
        calculate();
        invalidate();
    }

    public TextPaint getTextPaint() {
        return this.textPaint;
    }

    public void setTextPaint(TextPaint textPaint2) {
        this.textPaint = textPaint2;
    }

    public void setTextColor(int i) {
        getTextPaint().setColor(i);
        invalidate();
    }

    public void setLineSpacing(int i) {
        this.lineSpace = i;
        invalidate();
    }

    public int getTextColor() {
        return getTextPaint().getColor();
    }

    public int getLineSpace() {
        return this.lineSpace;
    }

    public Paint.Align getAlignment() {
        return getTextPaint().getTextAlign();
    }

    public void setAlignment(Paint.Align align) {
        getTextPaint().setTextAlign(align);
        invalidate();
    }
}
